package cn.org.donald.framework.base;

import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.pojo.vo.TableResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;

/**
 * @author ： Donald
 * @date ： 2020/10/21 01:09
 * @description：
 */
public class BaseController implements Serializable {

    private static final long serialVersionUID = 1L;

    public final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * success
     */


    /**
     * 直接返回成功
     * @return
     */
    public HttpResult toResultSuccess(){
        return this.toResultSuccess("请求成功");
    }

    /**
     * 只传数据
     * @param data
     * @return
     */
    public HttpResult toResultSuccess(Object data){
        return this.toResultSuccess(200,"请求成功",data);
    }

    /**
     * 只传提示信息
     * @param info
     * @return
     */
    public HttpResult toResultSuccess(String info){
        return this.toResultSuccess(200,info);
    }

    /**
     * 传状态码和提示信息
     * @param code
     * @param info
     * @return
     */
    public HttpResult toResultSuccess(Integer code, String info){
        return this.toResultSuccess(code,info,null);
    }

    /**
     * 传提示信息和数据
     * @param info
     * @param data
     * @return
     */
    public HttpResult toResultSuccess(String info, Object data){
        return this.toResultSuccess(200,info,data);
    }

    /**
     * 传状态码，提示信息，数据
     * @param code
     * @param info
     * @param data
     * @return
     */
    public HttpResult toResultSuccess(Integer code, String info, Object data){
        return new HttpResult(code,info,data);
    }


    /**
     * fail
     * @param info
     * @return
     */


    /**
     * 只传提示信息
     * @param infos
     * @return
     */
    public HttpResult toResultFail(String ... infos){
        StringBuilder sb = new StringBuilder();
        for (String s : infos) {
            sb.append(s);
        }
        String info = sb.toString();
        return this.toResultFail(500,info);
    }

    /**
     * 传状态码和提示信息
     * @param code
     * @param info
     * @return
     */
    public HttpResult toResultFail(Integer code, String info){
        return this.toResultFail(code,info,null);
    }

    public HttpResult toResultFail(Integer code, String info, Object data){
        return new HttpResult(code,info,data);
    }

    public TableResult toTableResult(Collection rows){
        long total = 0L;
        if (rows != null){
            total = rows.size();
        }
        return toTableResult(total,1L,rows);
    }

    public TableResult toTableResult(Long total, Long current, Collection rows){
        return new TableResult(total, 10L, current, rows);
    }

    public TableResult toTableResult(IPage page){
        return new TableResult(page.getTotal(),page.getSize(),page.getCurrent(),page.getRecords());
    }



}
